#! /usr/bin/env python
"""
::BOH
$Id: watcher.py,v 1.2 2006/04/18 00:30:00

Copyright (c) 2006 George Bobeck. All rights reserved.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
::EOH

Watcher.py is a non-daemon "collector".  
It listens for OWTF traffic and prints it to console.
"""
import signal
import sys
import pickle
from OWTFparser import *
from socket import *

# Notes on host:
# "" = localhost, will recieve MULTICAST datagrams
# "<address>" = binds to that address.  

host = ""
port = 50000
buf = 1024
addr = (host,port)

# Create socket and bind to address
UDPSock = socket(AF_INET,SOCK_DGRAM)
UDPSock.bind(addr)
def reading_printer(r):
    rout =  r.cron +"\t" + r.location + "\t" + r.sid + "\t" + str(r.temp) +"\t" +str( r.CtoF() )
    return rout

def signal_handler(signal,frame):
    UDPSock.close()
    print "Watcher Exiting."
    sys.exit( 1 )

signal.signal(signal.SIGINT, signal_handler)
print "Watcher Starting."
while 1:
    data,addr = UDPSock.recvfrom(buf)
    if not data:
        print "OOPS: Datagram Error!!"
        break
    else:
        try:
            dout = Reading()
            dout = pickle.loads(data)
            print reading_printer(dout)
            del dout
        except:
            print data